All files / Bandstand/src/app/(band)/setlists/[id] page.tsx

0% Statements 0/182
0% Branches 0/1
0% Functions 0/1
0% Lines 0/182

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                                                                                                                                                                                                                                                                                                                                                             
export const dynamic = 'force-dynamic'

import Link from 'next/link'
import {notFound} from 'next/navigation'
import {getSetlist} from '@/lib/setlists'
import {detectKey, parseChordPro} from '@/lib/chordpro'
import {displayName} from '@/lib/songs'
import {shortDate} from '@/lib/dates'
import SetlistPdfButton from '@/components/SetlistPdfButton'
import SaveOffline from '@/components/pwa/SaveOffline'
import {
  formatClock,
  parseClock,
  runningOrder,
  setSummary,
  timeRange,
} from '@/lib/gig'
import {pageSession} from '@/lib/guard'
import {followToBand} from '@/lib/band'

export const metadata = {title: 'Setlist'}

const gigFmt = new Intl.DateTimeFormat('en-US', {
  weekday: 'long',
  month: 'long',
  day: 'numeric',
  timeZone: 'UTC',
})

/** Read-only: the set in order, with keys and notes. Edit and Perform are buttons. */
export default async function SetlistView({params}: {params: {id: string}}) {
  const {user, band} = await pageSession()
  const set = await getSetlist(params.id, band.id)
  if (!set) {
    await followToBand('setlist', params.id, user.id, `/setlists/${params.id}`)
    notFound()
  }
  const order = runningOrder(set.startTime, set.items)
  const songs = order.entries.filter((e) => e.kind === 'SONG').length
  const start = parseClock(set.startTime)
  const songRow = (i: (typeof set.items)[number]) => {
    const v = i.song?.chartVersions[0]
    const chart = v ? parseChordPro(v.source) : null
    const original = chart ? detectKey(chart) : null
    return {
      key: i.key || original,
      changed: Boolean(i.key && original && i.key !== original),
      original,
      hasChart: Boolean(chart?.sections.length),
    }
  }
  return (
    <main className="mx-auto max-w-3xl px-4 pb-12 pt-4">
      <Link
        href="/setlists"
        className="text-sm text-muted no-underline hover:text-text"
      >
        ‹ Setlists
      </Link>
      <h1 className="mt-1 text-3xl font-extrabold">{set.name}</h1>
      <p className="mt-1 text-muted">
        {[
          set.gigDate && gigFmt.format(set.gigDate),
          start != null && formatClock(start, true),
          set.venue,
          `${songs} songs`,
        ]
          .filter(Boolean)
          .join(' · ')}
      </p>
      <p className="mt-0.5 text-[13px] text-faint">
        Edited by {displayName(set.updatedBy)} · {shortDate(set.updatedAt)}
      </p>

      <div className="mt-4 flex flex-wrap gap-2">
        <Link
          href={`/perform/${set.id}`}
          className="inline-flex min-h-11 items-center rounded-lg bg-accent px-4 font-extrabold text-on-accent no-underline"
        >
          ▶ Perform
        </Link>
        <SetlistPdfButton id={set.id} name={set.name} count={songs} />
        <SaveOffline
          urls={[
            `/perform/${set.id}`,
            ...new Set(
              set.items.flatMap((i) =>
                i.songId ? [`/songs/${i.songId}`] : [],
              ),
            ),
          ]}
        />
        <Link
          href={`/setlists/${set.id}/edit`}
          className="inline-flex min-h-11 items-center rounded-lg border border-line-2 px-4 no-underline"
        >
          Edit
        </Link>
      </div>

      {set.notes && (
        <p className="mt-5 whitespace-pre-wrap rounded-xl bg-panel px-4 py-3 text-[15px]">
          {set.notes}
        </p>
      )}

      <ol className="mt-5">
        {order.entries.map((e) => {
          if (e.kind === 'SET')
            return (
              <li
                key={e.item.id}
                className="mt-6 flex flex-wrap items-baseline gap-x-3 border-b-2 border-amber pb-1 first:mt-0"
              >
                <h2 className="text-lg font-extrabold">{e.info.label}</h2>
                <span className="font-mono">
                  {timeRange(e.info.start, e.info.end)}
                </span>
                <span className="text-sm text-muted">{setSummary(e.info)}</span>
              </li>
            )
          if (e.kind === 'BREAK')
            return (
              <li
                key={e.item.id}
                className="my-4 flex items-center gap-3 text-sm text-muted"
              >
                <span className="h-px flex-1 bg-line-2" />
                Break
                {e.info.minutes ? ` · ${e.info.minutes} min` : ''}
                {e.info.start != null &&
                  ` · ${timeRange(e.info.start, e.info.end)}`}
                <span className="h-px flex-1 bg-line-2" />
              </li>
            )
          const i = e.item
          const r = songRow(i)
          return (
            <li key={i.id} className="border-t border-line py-3">
              <div className="flex items-baseline gap-3">
                <span className="w-6 shrink-0 font-mono text-sm text-faint">
                  {e.n}
                </span>
                <span className="min-w-0 flex-1">
                  <Link
                    href={`/songs/${i.songId}`}
                    className="text-lg font-semibold no-underline hover:underline"
                  >
                    {i.song?.title}
                  </Link>
                  {!r.hasChart && (
                    <span className="ml-2 text-xs font-bold uppercase tracking-wider text-bad">
                      No chart
                    </span>
                  )}
                </span>
                {r.key && (
                  <span className="shrink-0 text-right font-mono font-bold text-amber">
                    {r.key}
                    {r.changed && (
                      <span className="block text-[11px] font-normal text-faint">
                        orig {r.original}
                      </span>
                    )}
                  </span>
                )}
              </div>
              {i.note && (
                <p className="mt-1 pl-9 text-[15px] text-warn-fg">{i.note}</p>
              )}
            </li>
          )
        })}
      </ol>
      {!songs && (
        <p className="py-8 text-muted">
          No songs yet. <Link href={`/setlists/${set.id}/edit`}>Add some</Link>.
        </p>
      )}
    </main>
  )
}